import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {RoleIdEnum} from "../../../enums/role-id.enum";
import {Consts} from "../../../config/consts";
import {BaseService} from "../../../shared/service/base.service";
import {ApiUrl} from "../../../config/api-url";

@Component({
  selector: 'app-account-info',
  templateUrl: './account-info.component.html',
  styleUrls: ['./account-info.component.less']
})
export class AccountInfoComponent implements OnInit {
  @Input()
  isVisible: boolean;
  @Input()
  moneyWithdraw: any;
  @Output()
  emit: EventEmitter<any> = new EventEmitter();
  // 发票信息
  InvoiceInfo: any;
  serverFileUrl: any;
  // 角色
  roleId: number;
  roleIdEnum = {
    SUPER_ADMINISTRATOR: RoleIdEnum.SUPER_ADMINISTRATOR,
    AGENT: RoleIdEnum.AGENT,
    MERCHANT: RoleIdEnum.MERCHANT,
  };

  constructor(public baseService: BaseService) {
  }

  ngOnInit(): void {
    this.findBackEndUrl();
    this.roleId = JSON.parse(sessionStorage.getItem(Consts.USER_INFO)).roleId;
    if (this.moneyWithdraw != null) {
      console.log(this.moneyWithdraw)
      this.findMoneyWithdrawInvoiceInfo();
    }
  }

  ngOnChanges() {
    if (this.moneyWithdraw != null) {
      this.InvoiceInfo = []
      this.findMoneyWithdrawInvoiceInfo();
    }
  }


  findBackEndUrl() {
    this.baseService.get(ApiUrl.Config + '/findBackEndUrl?type=image').subscribe(
      data => {
        if (data.status == 0) {
          if (data.data == null || data.data.url == null) {
            this.baseService.layer.messageError('找不到地址');
            return;
          }
          this.serverFileUrl = data.data.url;
        } else {
          this.baseService.layer.messageError(data.msg);
        }
      }
    );
  }

  handleCancel() {
    this.isVisible = false;
    this.emit.emit(this.isVisible);
  }

  checkInvoice(id) {
    this.baseService.layer.modalConfirm('您确定签收该发票！', confirm => {
      if (confirm) {
        let body = {
          'id': id,
          'signInStatus': 1
        }
        this.baseService.put(ApiUrl.MoneyWithdrawUrl + '/updateSignInInvoice', body).subscribe(data => {
            this.moneyWithdraw.signInStatus = 1;
        });
      }
    });
  }

  findMoneyWithdrawInvoiceInfo() {
    this.baseService.get(ApiUrl.MoneyWithdrawInvoiceInfoUrl + '/findMoneyWithdrawInvoiceInfo', this.moneyWithdraw.id).subscribe(data => {
      this.InvoiceInfo = data.data;
    });
  }
}
